package org.ft;

import com.baomidou.mybatisplus.core.parser.ISqlParser;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler;
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser;
import com.ft.core.annotation.FTSpringBootApplication;
import com.ft.core.utils.SpringContextHolder;
import com.ft.mybatis.annotation.EnableDruidStat;
import com.ft.security.annotation.EnableFTSecurity;
import com.ft.swagger.annotation.EnableFTSwagger2;
import com.ft.web.annotation.EnableApiResponse;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

import java.util.ArrayList;
import java.util.List;

/**
 * Hello world!
 * @author Lee
 */
@FTSpringBootApplication
@MapperScan("org.ft.domain.repositories")
@EnableFeignClients("org.ft.service.feign")
@EnableFTSwagger2
@EnableDruidStat
@EnableApiResponse
@EnableFTSecurity
public class StartApplication {
    public static void main(String[] args) {
        SpringApplication.run(StartApplication.class, args);

        // 注入多租户
        List<ISqlParser> sqlParserList = new ArrayList<>();
        TenantSqlParser tenantSqlParser = new TenantSqlParser();
        tenantSqlParser.setTenantHandler(new TenantHandler() {

            @Override
            public Expression getTenantId() {
                return new LongValue(1L);
            }

            @Override
            public String getTenantIdColumn() {
                return "tenant_id";
            }

            @Override
            public boolean doTableFilter(String tableName) {
                // 这里可以判断是否过滤表
                /*if ("user".equals(tableName)) {
                    return true;
                }*/
                return false;
            }
        });

        sqlParserList.add(tenantSqlParser);

        final PaginationInterceptor paginationInterceptor = SpringContextHolder.getBean(PaginationInterceptor.class);
        paginationInterceptor.setSqlParserList(sqlParserList);
        //paginationInterceptor.setSqlParserFilter(new ISqlParserFilter() {
        //
        //    @Override
        //    public boolean doFilter(MetaObject metaObject) {
        //        MappedStatement ms = SqlParserHelper.getMappedStatement(metaObject);
        //        // 过滤自定义查询此时无租户信息约束【 麻花藤 】出现
        //        if ("com.baomidou.springboot.mapper.UserMapper.selectListBySQL".equals(ms.getId())) {
        //            return true;
        //        }
        //        return false;
        //    }
        //});
    }

    /**
     * SQL执行效率插件
     * TODO 发布时去掉
     */
    @Bean
    public PerformanceInterceptor performanceInterceptor() {
        return new PerformanceInterceptor();
    }
}

